/**
 * Copyright &copy; 2012-2016 <a href="https://www.kaaction.com">KAA</a> All rights reserved All rights reserved.
 */
package com.kaaction.modules.mer.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.kaaction.common.config.Global;
import com.kaaction.common.persistence.Page;
import com.kaaction.common.web.BaseController;
import com.kaaction.common.utils.StringUtils;
import com.kaaction.modules.mer.entity.OperationProductDetail;
import com.kaaction.modules.mer.service.OperationProductDetailService;

/**
 * 商品运营明细Controller
 * @author huanghaiming
 * @version 2017-07-06
 */
@Controller
@RequestMapping(value = "${adminPath}/mer/operationProductDetail")
public class OperationProductDetailController extends BaseController {

	@Autowired
	private OperationProductDetailService operationProductDetailService;
	
	@ModelAttribute
	public OperationProductDetail get(@RequestParam(required=false) String id) {
		OperationProductDetail entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = operationProductDetailService.get(id);
		}
		if (entity == null){
			entity = new OperationProductDetail();
		}
		return entity;
	}
	
	@RequiresPermissions("mer:operationProductDetail:view")
	@RequestMapping(value = {"list", ""})
	public String list(OperationProductDetail operationProductDetail, HttpServletRequest request, HttpServletResponse response, Model model) {
		operationProductDetailService.updateOpeProtitle(operationProductDetail);
		Page<OperationProductDetail> page = operationProductDetailService.findPage(new Page<OperationProductDetail>(request, response), operationProductDetail); 
		model.addAttribute("page", page);
		return "modules/mer/operationProductDetailList";
	}

	@RequiresPermissions("mer:operationProductDetail:view")
	@RequestMapping(value = "form")
	public String form(OperationProductDetail operationProductDetail, Model model) {
		model.addAttribute("operationProductDetail", operationProductDetail);
		return "modules/mer/operationProductDetailForm";
	}

	@RequiresPermissions("mer:operationProductDetail:edit")
	@RequestMapping(value = "save")
	public String save(OperationProductDetail operationProductDetail, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, operationProductDetail)){
			return form(operationProductDetail, model);
		}
		operationProductDetailService.save(operationProductDetail);
		addMessage(redirectAttributes, "保存商品运营明细成功");
		return "redirect:"+Global.getAdminPath()+"/mer/operationProductDetail/?repage";
	}
	
	@RequiresPermissions("mer:operationProductDetail:edit")
	@RequestMapping(value = "delete")
	public String delete(OperationProductDetail operationProductDetail, RedirectAttributes redirectAttributes) {
		operationProductDetailService.delete(operationProductDetail);
		addMessage(redirectAttributes, "删除商品运营明细成功");
		return "redirect:"+Global.getAdminPath()+"/mer/operationProductDetail/?repage&opeproId="+operationProductDetail.getOpeproId()+"&opeProtitle="+operationProductDetail.getOpeProtitle();
	}

	@RequiresPermissions("mer:operationProductDetail:edit")
	@RequestMapping(value = "insertOperaProDtl")
	@ResponseBody
	public Map<String,Object> insertOperaProDtl(String id, String opeproId) {
		Map<String,Object> map = new HashMap<String,Object>();
		operationProductDetailService.insertOperaProDtl(id,opeproId);
		map.put("msg", "添加成功");
		return map;
	}
}